Component({
    properties: {},
    data: {
        showPicker: false,
        pickerSelectedValue: [0, 0],
        provinces: ['湖北省', '湖南省', '广东省'],
        cities: [
            ['武汉市', '黄冈市', '黄石市', '武穴市', '襄阳市', '荆门市', '天门市'],
            ['长沙市', '岳阳市', '娄底市'],
            ['深圳市', '广州市', '汕头市']
        ],
        selectedValue: []
    },
    methods: {
        onLoad() {
            // todo 获取定位，取值；这里先写死武汉市
            const selectedValue = ['湖北省', '武汉市']
            this.setData({
                selectedValue
            })
        },
        showPicker() {
            this.setData({
                showPicker: true
            });
        },
        pickerChange(e) {
            // 1. 获取当前选择 picker 的值
            const currentSelectedValue = e.detail.value
            // 2. 获取上次 picker 的值
            const lastSelectedValue = this.data.pickerSelectedValue
            // 3. 左侧的省份发生变化时，右边的城市重头从第一个显示
            if (currentSelectedValue[0] !== lastSelectedValue[0]) {
                this.setData({
                    pickerSelectedValue: [currentSelectedValue[0], 0],
                    selectedValue: [currentSelectedValue[0], 0]
                })
            } else {
                // 如果左侧的省份没有发生变化，说明用户切换的是右侧的城市
                this.setData({
                    pickerSelectedValue: currentSelectedValue
                })
            }
        },
        cancelPicker() {
            this.setData({
                showPicker: false
            });
        },
        confirmPicker() {
            const currentSelectedValue = this.data.pickerSelectedValue
            const province = this.data.provinces[currentSelectedValue[0]]
            const city = this.data.cities[currentSelectedValue[0]][currentSelectedValue[1]]
            this.setData({
                showPicker: false,
                selectedValue: [province, city]
            })
        }
    }
});
